package com.wx.example.demo.stream;

import java.util.*;
import java.util.function.BinaryOperator;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * @author wangxin
 * @time 2018/7/18 16:51
 */

public class Test {

    public static void main(String[] args) {

        System.out.println("8以前构建输出方式------------------------");
        //8以前
        List<Integer> oldList = Arrays.asList(6, 2, 3, 10, 22, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11);
        for (int i = 0; i < oldList.size(); i++) {
            Integer integer = oldList.get(i);
            System.out.println(integer);
        }
        System.out.println("8以前构建输出方式------------------------");

        System.out.println("9、10构建输出方式------------------------");
        //9 List.of Set.of 等 ； var 10
        var list = List.of(6, 2, 3, 10, 22, 1, 2, 3, 4, 5, 2, 7, 8, 9, 10, 11);
        list.forEach(System.out::println);
        System.out.println("9、10以前构建输出方式------------------------");

        System.out.println("Streams中的findAny findFirst方法------------------------");
        var any = list.stream().findAny();
        System.out.println(any);
      /*  while(any.get() == 6){   //一直返回6
            System.out.println(any);
            any = list.stream().findAny();
        }*/
        System.out.println(list.stream().findFirst());
//        list.stream()/*.filter(i->i%3==1)*/.dropWhile(i->i%3==1).forEach(i-> System.out.println(i));
        System.out.println("Streams中的findAny findFirst方法------------------------");

        //排序 遍历

        System.out.println("Streams中对集合排序，不会影响原集合------------------------");
//        Collections.sort(list); //以前版本,不可变集合不能排序
        list.stream().sorted().forEach(System.out::println);
        System.out.println("Streams中对集合排序，不会影响原集合------------------------");

        System.out.println("Streams构建集合输出------------------------");
        //流构建，流只能有一个终止操作
        var stream = Stream.iterate(1, i -> i < 10, i -> i + 1);
        stream.forEach(i -> System.out.println(i));
        System.out.println("Streams构建集合输出------------------------");

        System.out.println("Streams求集合最大值，过滤求和------------------------");
        //查找最大值
        var max = list.stream().max(Integer::compareTo);
        System.out.println(max);
        //过滤求和
        var sum = list.stream().filter(i -> i < 3).reduce(0, (a, b) -> a + b);
        System.out.println(sum);
        System.out.println("Streams求集合最大值，过滤求和------------------------");


        System.out.println("Streams构建复杂对象集合过滤输出------------------------");
        //流式构建集合
        var users = Stream.iterate(new User("wx", 0, "f"), i -> i.getAge() < 10, user -> {
            User u = new User();
            u.setAge(user.getAge() + 1);
            u.setName(user.getName() + u.getAge());
            u.setSex(u.getAge() % 2 == 0 ? "f" : "m");
            return u;
        }).collect(ArrayList<User>::new, List::add, (l, r) -> {
        });//.collect(Collectors.toList())

        //过滤 遍历
        users.stream().filter(user -> user.getAge() < 8).forEach(System.out::println);
        System.out.println("Streams构建复杂对象集合过滤输出------------------------");

        System.out.println("8以前对集合的过滤分组------------------------");
        //以前分组，按照性别分组,按照年龄过滤
        Map<String, List<User>> groupMap = new HashMap<>();
        for (int i = 0; i < users.size(); i++) {
            User user = users.get(i);
            if (user.getAge() < 6) {
                if (!groupMap.containsKey(user.getSex())) groupMap.put(user.getSex(), new ArrayList<>());
                groupMap.get(user.getSex()).add(user);
            }
        }
        groupMap.forEach((k, v) -> {
            System.out.print(k + ":");
            v.forEach(System.out::print);
            System.out.println();
        });
        System.out.println("8以前对集合的过滤分组------------------------");

        System.out.println("8以后对集合的过滤分组------------------------");
        //分组，按照性别分组,按照年龄过滤
        var groups = users.stream().filter(user -> user.getAge() < 6).collect(Collectors.groupingBy(User::getSex));
        //map 遍历
        groups.forEach((k, v) -> {
            System.out.print(k + ":");
            v.forEach(System.out::print);
            System.out.println();
        });
        System.out.println("8以后对集合的过滤分组------------------------");

        System.out.println("8以后对集合的分组统计------------------------");
        //按性别分组并统计年龄信息
        var groupsSum = users.stream().collect(Collectors.groupingBy(User::getSex, Collectors.summarizingInt(User::getAge)));
        groupsSum.forEach((k, v) -> {
            System.out.print(k + ":");
            System.out.print(v);
            System.out.println();
        });
        System.out.println("8以后对集合的分组统计------------------------");

        System.out.println("Strams转换遍历------------------------");
        //转换流
        users.stream().mapToInt(User::getAge).forEach(System.out::println);
        System.out.println("Strams转换遍历------------------------");

        System.out.println("自定义Lambda表达式及Optional类简单运用------------------------");
        //自定义lambda方法
        var convert = convert(1, Optional::ofNullable);
        convert.ifPresentOrElse(System.out::println, () -> System.out.println(100));

        //以前匿名类
        var convertOld = convert(1, new Lambda<Integer, Optional<Integer>>() {
            @Override
            public Optional<Integer> convert(Integer t) {
                return Optional.of(t);
            }
        });
        //Optional类简单运用，非空判断
        convertOld.ifPresentOrElse(System.out::println, () -> System.out.println(100));
        System.out.println("自定义Lambda表达式及Optional类简单运用------------------------");

    }

    public static <T, R> R convert(T t, Lambda<T, R> lambda) {
        return lambda.convert(t);
    }

    /*
     *
     * 建议大家可以多看看以S结尾的类，比如Objects,Files,Arrays,Collections,Collectors这些工具类
     *
     *
     * */

}
